import { Loading, Message } from "element-ui";

/***
 * R-ZBZB-202109280789713：添加下载图标，增加下载列表页面
 */
const baseUrl = process.env.VUE_APP_BASE_API;
import request from "@/utils/request";
import axios from "axios";
import { getToken } from "@/utils/common";
const mimeMap = {
  xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
  zip: "application/zip",
  pdf: "application/pdf",
};
export const service = () => {
  return "/event";
};
export const device = () => {
  return "/device";
};
export const manage = () => {
  return "/manage";
};
const event = "/event";
const receive = "/receive";

// 查询租户信息
export function queryTenantInfo(query) {
  return request({
    url: "/admin/custom/manage/queryTenantInfo",
    method: "post",
    data: query,
  });
}
export function getSeqByName(query) {
  return request({
    url: "admin/common/getSeqByName/flow_link_seq/" + query.count,
    method: "get",
  });
}
//'/video/getRegion'
export function getRegion(query) {
  return request({
    url: "/admin/video/getRegion",
    method: "post",
    data: query,
  });
}

//'/video/warningEvent/getDeviceList'
export function getDeviceList(query) {
  return request({
    url: device() + "/video/device/getDeviceList",
    method: "post",
    data: query,
  });
}
//'/video/warningEvent/getDeviceList'
export function queryTenant(query) {
  return request({
    url: "/admin/system/tenant/queryTenant",
    method: "post",
    data: query,
  });
}
//queryTenantLabelList
export function queryTenantLabelList(query) {
  return request({
    url: "/device/video/device/queryTenantLabelList",
    method: "post",
    data: query,
  });
}
//'/alarm/processorder/grabProcessOrder'
export function grabProcessOrder(query) {
  return request({
    url: service() + "/alarm/processorder/grabProcessOrder",
    method: "post",
    data: query,
  });
}
//'/alarm/order/getOrderList'
export function getOrderList(query) {
  return request({
    url: service() + "/alarm/order/getOrderList",
    method: "post",
    data: query,
  });
}
//'/alarm/type/findOrderAlarm'
export function findOrderAlarm(query) {
  return request({
    url: service() + "/alarm/type/findOrderAlarm",
    method: "post",
    data: query,
  });
}
export function findAlgorithmAlarm(query) {
  return request({
    url: service() + "/alarm/type/findAlgorithmAlarm",
    method: "post",
    data: query,
  });
}
//'/alarm/type/findOrderAlarm'
export function findOrderAlarmTT(query) {
  return request({
    url: "/event/alarm/type/findOrderAlarm",
    method: "post",
    data: query,
  });
}
//'/system/getEnumInfo'
// 告警来源下拉数据用户端单独处理
export function getEnumInfo(query) {
  if (query.enumTypeId === "warning_source_common" && service() === "/event") {
    return request.post("/event/alarm/flowconfig/findAlarmSourceAll");
  }
  return request({
    url: "/admin/base/system/dict/data/dictType/" + query.enumTypeId,
    method: "get",
  });
}
export function findAlarmSourceAll(query) {
  return request({
    url: "/event/alarm/flowconfig/findAlarmSourceAll",
    method: "post",
    data: query,
  });
}
///system/custorgan/getCustOrganTreeNew
export function getCustOrganTreeNew(query) {
  return request({
    url: "/admin/system/tenantDept/listSubsetDept",
    method: "post",
    data: query,
  });
}

//
export function addAlarmOrder(query) {
  return request({
    url: "/receive/alarm/receive/addAlarmOrder",
    method: "post",
    data: query,
  });
}
// 批量研判
export function batchJudgeWarningOrder(query) {
  return request({
    url: event + "/alarm/orderBatch/batchJudgeWarningOrder",
    method: "post",
    data: query,
  });
}
//运营研判---批量研判（支持真假同时存在）
export function operateBatchJudgeWarningOrder(query) {
  return request({
    url: event + "/alarm/orderBatch/operateBatchJudgeWarningOrder",
    method: "post",
    data: query,
  });
}
// 批量删除
export function batchDelWarningOrder(query) {
  return request({
    url: event + "/alarm/orderBatch/batchDelWarningOrder",
    method: "post",
    data: query,
  });
}
// 批量关闭
export function batchEndWarningOrder(query) {
  return request({
    url: event + "/alarm/orderBatch/batchEndWarningOrder ",
    method: "post",
    data: query,
  });
}
//校验当前告警是否还在此环节
export function getCurrentByOrderIdAndLinkId(query) {
  return request({
    url: event + "/flow/order/getCurrentByOrderIdAndLinkId",
    method: "post",
    data: query,
  });
}
//
export function querySysLinkViewUrl(query) {
  return request({
    url: "/ttmanage" + "/industry/link/querySysLinkViewUrl",
    method: "post",
    data: query,
  });
}
/**
 * 获取所有告警标签
 * */
export function getAllLables() {
  return request({
    url: "/event/alarm/lable/getAllLables",
    method: "get",
  });
}
/**
 * 根据经纬度查询网格以及网格员信息
 * */
export function getGridInfoByLongitudeAndLatitude(data) {
  return request({
    url: service() + "/alarm/order/getGridInfoByLongitudeAndLatitude",
    method: "post",
    data: data,
  });
}
/**
 * 查询关联事件
 * */
export function getWarningOrderInfoRel(data) {
  return request({
    url: "/event" + "/alarm/orderForTrade/warningOrderInfoRel",
    method: "post",
    data: data,
  });
}
/**
 * 查询告警事件详情
 * */
export function getOrderInfo(data) {
  return request({
    url: "/event" + "/alarm/orderForTrade/getOrderInfo",
    method: "post",
    data: data,
  });
}
/**
 * 查询下一环节流程
 * */
export function queryNextLink(data) {
  return request({
    url: "/event" + "/api/flow/alarm/queryNextLink",
    method: "post",
    data: data,
  });
}
/**
 * 查询查询当前环节
 * */
export function queryFlowLink(data) {
  return request({
    url: "/event" + "/api/flow/alarm/queryFlowLink",
    method: "post",
    data: data,
  });
}
/**
 *  查询组织人员树（领导）
 * */
export function getLeaderByTenantIdShowTree(data) {
  return request({
    // url: "/event"+"/alarm/orderForTrade/getLeaderByTenantIdShowTree",
    url: "/event" + "/video/base/getLeaderByTenantIdShowTree",
    method: "post",
    data: data,
  });
}
/**
 *  查询组织人员树
 * */
export function getFlowByIdUserTreeNew(data) {
  return request({
    url: "/event" + "/flow/alarm/getFlowByIdUserTree",
    method: "post",
    data: data,
  });
}
/**
 *  查询详情关联标签
 * */
export function getLableByAlarm(id, data) {
  return request({
    url: "/event" + "/alarm/lable/getLableByAlarm/" + id,
    method: "get",
    data: data,
  });
}
/**
 *  获取网格员
 * */
export function getGridInfoBy(data) {
  return request({
    url: "/event" + "/alarm/order/getGridInfoByLongitudeAndLatitude",
    method: "post",
    data: data,
  });
}
/**
 *  获取告警类型
 * */
export function findOrderAlarmEvent(data) {
  return request({
    url: "/event" + "/alarm/orderForTrade/findOrderAlarm",
    method: "post",
    data: data,
  });
}
/**
 *  收藏事件
 * */
export function warningOrderCollection(data) {
  return request({
    url: "/event" + "/alarm/orderForTrade/warningOrderCollection",
    method: "post",
    data: data,
  });
}
/**
 *  获取告警的网格
 * */
export function findOneSdk(data) {
  return request({
    url: "/bdm" + "/grid/findOneSdk",
    method: "post",
    data: data,
  });
}
/**
 *  查询回退环节
 * */
export function queryRollbackLink(data) {
  return request({
    url: "/event" + "/flow/order/queryRollbackLink",
    method: "post",
    data: data,
  });
}
/**
 *  查询可转派环
 * */
export function queryReassignLink(data) {
  return request({
    url: "/event" + "/alarm/orderForTrade/queryReassignLink",
    method: "post",
    data: data,
  });
}
/**
 *  回退环节
 * */
export function rollBack(data) {
  return request({
    url: "/event" + "/alarm/processorder/rollBack",
    method: "post",
    data: data,
  });
}
/**
 *  回退环节
 * */
export function reassignment(data) {
  return request({
    url: "/event" + "/alarm/orderForTrade/reassignment",
    method: "post",
    data: data,
  });
}
/**
 *  查询重复告警列表
 * */
export function getOrderListForDuplication(data) {
  return request({
    url: "/event" + "/alarm/orderForTrade/getOrderListForDuplication",
    method: "post",
    data: data,
  });
}
/**
 *  提交领导批示
 * */
export function addLeaderAudit(data) {
  return request({
    url: "/event" + "/alarm/orderForTrade/addLeaderAudit",
    method: "post",
    data: data,
  });
}

export function createIntercept(data) {
  return request({
    url: "/event" + "/alarm/orderForTrade/createIntercept",
    method: "post",
    data: data,
  });
}
/**
 *  关闭告警
 * */
export function batchEndOrder(data) {
  return request({
    url: "/event" + "/alarm/orderBatch/batchEndWarningOrder",
    method: "post",
    data: data,
  });
}
/**
 *  新增告警分享
 * */
export function addShare(data) {
  return request({
    url: "/event" + "/alarm/share/addShare",
    method: "post",
    data: data,
  });
}
/**
 *  新增告警分享
 * */
export function confirmAccept(data) {
  return request({
    url: "/event" + "/alarm/order/accept",
    method: "post",
    data: data,
  });
}
/**
 *  获取所有标签信息
 * */
export function getAllTags(data) {
  return request({
    // url: "/event"+"/alarm/tag/getAllTags",
    url: "/event" + "/alarm/lable/getAllLables",
    method: "get",
    data: data,
  });
}
/**
 *  删除标签
 * */
export function deleteTag(data) {
  return request({
    // url: "/event"+"/alarm/tag/deleteTag",
    url: "/event" + "/alarm/lable/deleteLable",
    method: "post",
    data: data,
  });
}
/**
 *  提交关联标签信息
 * */
export function saveTagRelation(data) {
  return request({
    // url: "/event"+"/alarm/tag/saveTagRelation",
    url: "/event" + "/alarm/lable/saveLableRelation",
    method: "post",
    data: data,
  });
}
/**
 *  编辑标签
 * */
export function modifyTag(data) {
  return request({
    // url: "/event"+"/alarm/tag/modifyTag",
    url: "/event" + "/alarm/lable/modifyLable",
    method: "post",
    data: data,
  });
}
/**
 *  新增标签
 * */
export function addTag(data) {
  return request({
    // url: "/event"+"/alarm/tag/addTag",
    url: "/event" + "/alarm/lable/addLable",
    method: "post",
    data: data,
  });
}
/**
 *  提交流程
 * */
export function submitProcess(data) {
  return request({
    url: "/event" + "/alarm/orderForTrade/submitProcess",
    method: "post",
    data: data,
  });
}
//误报
export function checkAlarmOrder(data) {
  return request({
    url: "/event" + "/alarm/order/checkAlarmOrder",
    method: "post",
    data,
  });
}
/**
 *  获取常用语
 * */
export function getProcessCommonWords(data) {
  return request({
    url: "/event" + "/commonWords/getProcessCommonWords",
    method: "post",
    data: data,
  });
}
/**
 *  编辑常用语
 * */
export function updateProcessCommonWords(data) {
  return request({
    url: "/event" + "/commonWords/updateProcessCommonWords",
    method: "post",
    data: data,
  });
}
/**
 *  编辑常用语
 * */
export function fileUpload(data) {
  return request({
    url: "/event" + "/admin/base/common/upload",
    method: "post",
    data: data,
  });
}
/**
 *  查询预案区域
 * */
export function queryTenantTree(data) {
  return request({
    url: "/admin" + "/base/system/tenantDept/getTenantTree",
    method: "post",
    data: data,
  });
}
// 图片上传
export function uploadImg(data) {
  return request({
    url: "/file/base/common/upload",
    method: "post",
    data: data,
  });
}

/**
 * 解析blob响应内容并下载
 * @param {*} res blob响应内容
 * @param {String} mimeType MIME类型
 * @param {String} fileName 文件名
 */
export function resolveBlob(res, mimeType, fileName) {
  const aLink = document.createElement("a");
  const blob = new Blob([res.data], { type: mimeType });
  aLink.href = URL.createObjectURL(blob);
  aLink.setAttribute("download", fileName); // 设置下载文件名称
  document.body.appendChild(aLink);
  aLink.click();
  document.body.removeChild(aLink);
}
/**
 * 通用压缩文件下载方法(post方式)
 * @param url
 * @param fileName
 */
export function downLoadZipPost(url, fileName, data, callback, suffix = "zip") {
  // let loadingInstance = Loading.service({
  //   lock: true,
  //   text: "数据正在准备中，请稍等...",
  //   background: "rgba(0, 0, 0, 0.7)"
  // });
  let industryCode = "";
  const ddw = sessionStorage.getItem("chooseTradeVersionddw");
  const dd2 = sessionStorage.getItem("chooseTradeVersiondd2");
  const dd = JSON.parse(
    sessionStorage.getItem("chooseTradeVersiondd") || null
  )?.industryCode;

  if (url === "/auth/chooseTrade") {
    industryCode = ddw || "";
  } else {
    industryCode = dd2 || dd || "";
  }
  let headers = {
    Authorization: "Bearer " + getToken(),
    "Cur-Industry-Code": industryCode,
  };
  if (data instanceof FormData) {
    headers["Content-Type"] = "multipart/form-data";
  }
  axios({
    method: "post",
    url: baseUrl + url,
    data: data,
    responseType: "blob",
    headers: headers,
  })
    .then((res) => {
      // loadingInstance.close();
      if (res.data.type === "application/json") {
        const reader = new FileReader();
        reader.readAsText(res.data, "utf-8");
        reader.onload = function () {
          let resp = JSON.parse(reader.result);
          Message({
            message: resp.msg,
            type: "warning",
          });
        };
        return;
      }
      if (callback) {
        callback(true);
      }
      resolveBlob(res, mimeMap.zip, (fileName || "defaultName") + "." + suffix);
    })
    .catch((e) => {
      if (callback) {
        callback(false);
      }
    });
}
// 属地告警，事件查询
export function getLocalOrderList(data) {
  return request({
    url: "/ttevent/alarm/order/local/getOrderList",
    method: "post",
    data,
  });
}
// 属地告警，回收站查询
export function getLocalInterceptList(data) {
  return request({
    url: "/ttevent/alarm/order/local/getInterceptList",
    method: "post",
    data,
  });
}
// 属地告警，运营研判查询
export function getLocalOperateList(data) {
  return request({
    url: "/ttevent/alarm/order/local/getOperateList",
    method: "post",
    data,
  });
}
//获取节点列表
export function queryListByKey(query) {
  return request({
    url: "/cascade/platformData/queryListByKey",
    method: "post",
    data: query,
  });
}
// 获取告警原因列表
export function getAlarmReasonList(query) {
  return request({
    url: service() + "/alarm/reason/list",
    method: "post",
    data: query,
  });
}
//根据告警类型查询告警原因
export function getOrderReason(data) {
  return request({
    url: "/event" + "/alarm/reason/list",
    method: "post",
    data: data,
  });
}

// 物联设备下拉列表选择
export function getIotDeviceList(query) {
  return request({
    url: device() + "/video/device/getIotDeviceList",
    method: "post",
    data: query,
  });
}
// 查询告警原因列表
export function queryAlarmReasonList(query) {
  return request({
    url: "/event/alarm/reason/queryAlarmReasonList",
    method: "post",
    data: query,
  });
}

// 查询图斑信息
export function getXjxlFeaturesByIds(params) {
  return request({
    url: "/gis/getFeatures/getXjxlFeaturesByIds",
    method: "post",
    data: params,
  });
}
